package com.powernode.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.constant.ProductConstants;
import com.powernode.domain.Category;
import com.powernode.ex.handler.BusinessException;
import com.powernode.mapper.CategoryMapper;
import com.powernode.service.CategoryService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 贺
 * @description 针对表【category(产品类目)】的数据库操作Service实现
 * @createDate 2024-07-18 13:24:14
 */
@Service
@CacheConfig(cacheNames = "com.powernode.service.impl.CategoryServiceImpl")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 查询所有类目
     *
     * @return
     */
    @Override
    @Cacheable(key = ProductConstants.ALL_CATEGORY_LIST_KEY)
    public List<Category> queryAllCategoryList() {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>().orderByDesc(Category::getSeq));
    }

    /**
     * 查询系统商品一级类目
     *
     * @return
     */
    @Override
    @Cacheable(key = ProductConstants.FIRST_CATEGORY_LIST_KEY)
    public List<Category> queryFirstCategoryList() {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId, 0)
                .eq(Category::getStatus, 1)
                .orderByDesc(Category::getSeq));
    }

    /**
     * 新增商品类目
     *
     * @param category
     * @return
     */
    @Override
    @CacheEvict(allEntries = true)
    /*@Caching(evict = {
            @CacheEvict(key = ProductConstants.ALL_CATEGORY_LIST_KEY),
            @CacheEvict(key = ProductConstants.FIRST_CATEGORY_LIST_KEY)
    })*/
    public Boolean saveCategory(Category category) {
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        return categoryMapper.insert(category) > 0;
    }

    /**
     * 修改商品类目
     *
     * @param category
     * @return
     */
    @Override
    @CacheEvict(allEntries = true)
    public Boolean modifyCategory(Category category) {
        // 根据类目ID查询类目详情
        Category oldCategory = categoryMapper.selectById(category.getCategoryId());
        // 获取修改前的类目级别，如果parentId为0，则表示一级类目，否则表示二级类目
        Long oldParentId = oldCategory.getParentId();
        // 获取修改后的类目级别
        Long newParentId = category.getParentId();
        // 判断商品类目修改前后的类目级别是否发生变化
        if (0 == oldParentId && null != newParentId && 0 != newParentId) {
            // 修改前的类目级别为1级，修改后的类目级别为2级，判断当前类目是否包含子类目，如果包含子类目，则不允许修改
            // 根据当前类目ID查询子类目集合
            List<Category> childrenList = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                    .eq(Category::getParentId, category.getCategoryId()));
            // 判断子类目集合是否为空
            if (CollectionUtil.isNotEmpty(childrenList) && childrenList.size() > 0) {
                // 当前要从一级修改为二级的类目中包含子类目，不允许修改
                throw new BusinessException("当前类目包含子类目，不允许修改");
            }
        }

        if (0 != oldParentId && null == newParentId) {
           // 修改前的类目级别为2级，修改后的类目级别为1级
            category.setParentId(0L);
        }

        category.setUpdateTime(new Date());
        return categoryMapper.updateById(category) > 0;
    }

    /**
     * 删除商品类目
     * 如果一级类目包含子类目，不可删除
     * 根据类目ID查询子类目集合，如果子类目集合不为空，则不允许删除
     *
     * @param categoryId
     * @return
     */
    @Override
    @CacheEvict(allEntries = true)
    public Boolean removeCategoryById(Long categoryId) {
        // 根据类目ID查询子类目集合
        List<Category> childrenList = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId, categoryId));
        // 判断子类目集合是否为空
        if (CollectionUtil.isNotEmpty(childrenList) && childrenList.size() > 0) {
            // 当前类目包含子类目，不允许删除
            throw new BusinessException("当前类目包含子类目，不允许删除");
        }
        // 待删除类目不包含子类目，可以删除
        return categoryMapper.deleteById(categoryId) > 0;
    }

    /**
     * 查询小程序中商品的一级类目集合
     *
     * @param parentId
     * @return
     */
    @Override
    @Cacheable(key = ProductConstants.WX_FIRST_CATEGORY_LIST_KEY)
    public List<Category> queryWxCategoryList(Long parentId) {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId, parentId)
                .eq(Category::getStatus, 1)
                .orderByDesc(Category::getSeq));
    }
}




